<?php
/**
 * Short tag Helper class file.
 *
 * Infectamic: browser game (http://infectamic.goldzoneweb.info)
 * Copyright 2011, Loic Chardonnet
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 */

/**
 * Short tag Helper class for easy use of JavaScript..
 *
 * ShortTagHelper encloses all methods needed while working with HTML short tags.
 */
class ShortTagHelper extends AppHelper {
    var $tags = array(
        'short_tag'  => '<%s%s/>',
    );

    var $linksAttributes = array(
        'src',
        'href',
    );

    /**
     * Returns a formatted short tag from the given name.
     *
     * @param string $name The name of the short tag.
     * @param array $options An associative array of HTML tag attributes.
     * @return string The formated short tag.
     */
    function generateFromName($name, $attributes = array()) {
        foreach ($attributes as $key => &$attribute) {
            if (true === in_array($key, $this->linksAttributes)) {
                $attribute = $this->webroot($attribute);
            }
        }
        return sprintf($this->tags['short_tag'], $name, $this->_parseAttributes($attributes, null, ' ', ' '));
    }

    /**
     * Returns a formatted link short tag.
     *
     * @param array $attributes An associative array of HTML tag attributes.
     * @return string The formated link short tag.
     */
    function link($attributes = array()) {
        return $this->generateFromName('link', $attributes);
    }

    /**
     * Returns a formatted meta short tag.
     *
     * @param array $attributes An associative array of HTML tag attributes.
     * @return string The formated link short tag.
     */
    function meta($attributes = array()) {
        return $this->generateFromName('meta', $attributes);
    }
}
